﻿using System;
using System.IO;
using System.Threading;

namespace 第三题
{
    class Program
    {
        public class FileHelper
        {
            public int threadh;

            public bool CopyFile(string fromPath, string toPath, int eachReadLength)
            {
                FileStream fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read);
                FileStream toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write);
                int toCopyLength = 0;
                Thread[] threads = new Thread[10];

                if (eachReadLength < fromFile.Length)
                {
                    byte[] buffer = new byte[eachReadLength];
                    long copied = 0;
                    for (int i = 0; copied <= fromFile.Length - eachReadLength; i++)
                    {
                        if (i < 10)
                        {
                            threads[i] = new Thread(() => Console.WriteLine("线程{" + i + "}进行"));
                            threads[i].Start();
                        }
                        toCopyLength = fromFile.Read(buffer, 0, eachReadLength);
                        fromFile.Flush();
                        toFile.Write(buffer, 0, eachReadLength);
                        toFile.Flush();
                        toFile.Position = fromFile.Position;
                        copied += toCopyLength;
                    }
                    int left = (int)(fromFile.Length - copied);
                    toCopyLength = fromFile.Read(buffer, 0, left);
                    fromFile.Flush();
                    toFile.Write(buffer, 0, left);
                    toFile.Flush();
                }
                else
                {
                    byte[] buffer = new byte[fromFile.Length];
                    fromFile.Read(buffer, 0, buffer.Length);
                    fromFile.Flush();
                    toFile.Write(buffer, 0, buffer.Length);
                    toFile.Flush();
                }

                fromFile.Close();
                toFile.Close();
                return true;
            }
        }
        static void Main(string[] args)
        {
            FileHelper file1 = new FileHelper();

            file1.CopyFile(@"D:\Student\c.txt", @"D:\Student\d.txt", 10);
            Console.WriteLine("Hello World!");
        }
    }
}
